<?php

/**
 * @package yet-another-glossary
 */

if ( !defined( 'YAG_POST_TYPE_NAME' ) ) {
	define('YAG_POST_TYPE_NAME', 'yag_glossary_words');
}
 
require_once( WP_PLUGIN_DIR . '/yet-another-glossary/includes/install.php');

require_once( WP_PLUGIN_DIR . '/yet-another-glossary/includes/views/admin_settings.php');
require_once( WP_PLUGIN_DIR . '/yet-another-glossary/includes/views/admin_post_view.php');
require_once( WP_PLUGIN_DIR . '/yet-another-glossary/includes/views/admin_page_include_glossary.php');

/**
 *
 */
function yaggerfy_the_admin() {
	
	add_submenu_page(
		'edit.php?post_type=yag_glossary_words', 
		'Glossary Admin', 
		'Settings', 
		'edit_posts', 
		basename(__FILE__), 
		'yag_view_admin_settings'
		);
	 
}

function yagger_up_a_metadata_box()
{
	add_meta_box( 
		'yag-glossary-word-metadata', 
		'Glossary Word Options', 
		'yag_view_glossary_word_metadata', 
		YAG_POST_TYPE_NAME, 
		$context = 'side', 
		$priority = 'default'
		);
}

function yag_glossary_word_metadata_save( $post_id, $post ) {
	
	if ( 	!isset( $_POST['yag_post_word_nonce'] ) || 
			!wp_verify_nonce( $_POST['yag_post_word_nonce'], 'admin_post_view.php' ) 
		) {
		return $post_id;
	}

	$display_option = $_POST['yag_definition_display_option'];
	$display_option_excerpt = $_POST['yag_definition_display_option_excerpt'];
	
	if( !$display_option_excerpt && $display_option == 'excerpt') {
		$display_option_excerpt['number'] = 50;
	}
	
	update_post_meta( $post_id, 'yag_definition_display_option', $display_option );
	update_post_meta( $post_id, 'yag_definition_display_option_excerpt', $display_option_excerpt );

	return $post_id;
}

function yag_glossary_setting_save( $post_id, $post ) {
	
	if ( 	!isset( $_POST['yag_settings_page'] ) ||
			!wp_verify_nonce( $_POST['yag_settings_page'], 'admin_settings.php' )
	) {
		return $post_id;
	}
	
	$option = array();
	$options['yag_on_page'] 								= $_REQUEST['yag_on_page'];
	$options['pretty_pretty_colors']['style_background'] 	= $_REQUEST['style_background'];
	$options['pretty_pretty_colors']['style_color'] 		= $_REQUEST['style_color'];
	$options['pretty_pretty_colors']['style_border_color'] 	= $_REQUEST['style_border_color'];
	
	update_option('yag_options', $options);

	return $post_id;
}

function yag_glossary_page_save( $post_id, $post ){

	if ( get_post_type( $post ) == 'page' ){
   		add_action('admin_notices', 'my_admin_notice');		
	}
}

function my_admin_notice(){
	echo '
		<div class="updated"><p>Remember, if you want to display glossary definitions on new 
				pages you need to select it in the Glossary Settings.</p>
		</div>';
}

function yagger_up_a_another_metadata_box()
{
	add_meta_box( 
		'yag-glossary-display-glossary', 
		'Display Glossary On Page', 
		'yag_view_glossary_on_page', 
		'page', 
		$context = 'side', 
		$priority = 'default'
		);
}

/**
 * Creates the glossary page when the tag [YAGlossaryPage] is present in the content.
 * Loops through all of the words and lays out there definition
 * 
 * @param string $content
 * @return string
 */
function yaggerfy_page() {
	
	$html = array();
	$yags = get_posts(array('post_type' => YAG_POST_TYPE_NAME));
	
	foreach($yags as $yag)
	{
		$word_list[$yag->post_title] = '<p class="yag_word_container">';
		$word_list[$yag->post_title].= '<span class="yag_word"><a name="' . stripslashes ( $yag->post_title ) . '">' . stripslashes(ucwords($yag->post_title)) .':</a></span> ';
		$word_list[$yag->post_title].= '<span class="yag_definition">' . stripslashes ( $yag->post_content ) . '</span></p>';
	}
	ksort($word_list);
	$html = implode("\n", $word_list);
	return  $html;
}

/**
 * Searches the content of post / pages for words in the glossary
 * If found, wraps the word in the needed html and outputs the definition 
 * of the word in a hidden div that will put at the end of the content
 * 
 * @param string $content
 * @return string
 * @todo Update this to use the custom posts
 * @todo streamline / update html to work with new qtip code...
 */
function yaggerfy_words($content)
{
	global $page;
	
	$yags = get_posts(array('post_type' => YAG_POST_TYPE_NAME));
	
	$defs = array();
	
	foreach ($options as $key => $option)
	{
		if ( stripos( $content, $option['keyword'] ) === false) {
			continue;
		}
		
		$replace = ' <span id="' . $key . '" class="word_' . $key . ' aYag"><a href="' . site_url() .'/glossary/#' . $option->post_title . '">' . stripslashes( $option->post_title ). '</a></span> ';
		
		$defs[] = '<div id="definition_' . $key . '" style="display:none;">' . stripslashes ( nl2br ( utf8_encode ( $option->post_content ) ) ) . '</div>';
		
		$content = preg_replace ( "/ ". $option->post_title ."/is", $replace ,$content );
	}
		
	return $content . implode ( "\n", $defs );
}

/**
 * Updates the head to include the jQuery (if not already present) and the 
 * qtip js plugin, which handles the tooltip feature of the plgin
 */
function yagged_in_the_hEEd()
{

	wp_deregister_script( 'jquery' );
	wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
	wp_enqueue_script( 'jquery' );
		
	if ( !is_admin() ) {
		wp_register_script( 'yag-borrowed-tipTip-JS', WP_PLUGIN_URL . '/yet-another-glossary/assets/js/tipTips.js' );
		wp_enqueue_script( 'yag-borrowed-tipTip-JS' );
		
		wp_register_style( 'yag-borrowered-tipTip-CSS', WP_PLUGIN_URL . '/yet-another-glossary/assets/css/tipTip.css');
    wp_enqueue_style( 'yag-borrowered-tipTip-CSS' );
	} else {
		wp_register_script( 'yag-borrowed-colorPicker-JS', WP_PLUGIN_URL . '/yet-another-glossary/assets/js/colorPicker.js' );
		wp_enqueue_script( 'yag-borrowed-colorPicker-JS' );
	
		wp_register_style( 'yag-borrowered-colorPicker-CSS', WP_PLUGIN_URL . '/yet-another-glossary/assets/css/colorPicker.css');
    wp_enqueue_style( 'yag-borrowered-colorPicker-CSS' );
	}

}

/**
 * svn checkout http://farbtastic.googlecode.com/svn/trunk/
 Builds the JS script that adds the hover event to all of the words
 * that were found in the glossary so the definition will be presented 
 * to the user on hover
 */
function yagger_up_js()
{
?>
	<script type="text/javascript">
	jQuery(document).ready(function() {
			jQuery('.aYag').each(function() {
					jQuery(this).qtip({
							overwrite: false,
							content: jQuery('#definition_' + this.id).html(),
			   				style: { padding: 5, background: '#FBFDFE', color: 'black', textAlign: 'left', border: { color: '#eee' } },
			    			position: { corner: { target: 'rightMiddle', tooltip: 'leftBottom' } }, 
			    			show: { ready: false }
						});
					});
	});
	</script>
<?php 
}

/**
 * @todo: Build out the abilty to mod css of tool tip box
 */
function yagger_up_css()
{
	
}